home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / mouse01g.zip / MOUSE1G.C < prev    next >
C/C++ Source or Header  |  1992-05-24  |  8KB  |  207 lines

  1. /* This code is designed to be inserted into a TURBO-C source. */
  2. /* Variable __TURBOC__ is defined by the turbo-c compiler. */
  3. /* Usage: Call mousereset(). Call mousehook(). Mouse setup done!
  4.  
  5.           If you enter a menu, where mouse action should be slower,
  6.           then call menumouse(1). On menu exit call menumouse(0).
  7.  
  8.           Upon program exit call mousereset(). This resets the
  9.           mouse driver (either a device or tsr from mouse manufacturer)
  10.           interrupt service routine [warning: REQUIRED!].
  11. */
  12. #ifdef __TURBOC__
  13. #pragma inline
  14. /*
  15.  * MOUSE SENSITIVITY. A curious feature in mouse code is sensitivity to
  16.  * movement: settling down on a character is subject to much error. In the
  17.  * code below this problem is attacked.
  18.  *
  19.  *     Ideally, the cursor should lock onto a line and not be bumped off
  20.  *     easily as the cursor scans left and right. Further, up and down
  21.  *     motion should lock the cursor onto a column. I should like to scan
  22.  *     up and down columns of numbers with the mouse exactly as I do with
  23.  *     the cursor keys. The code below is an attempt to realize these
  24.  *     features that falls short of perfection.
  25.  *
  26.  * MOUSE DRIVER DEFAULTS. The TSR resets the mouse driver and but sets no
  27.  * defaults for the mouse controls: X,Y sensitivity, doubler.
  28.  */
  29.  
  30. #define leftkey                 4b00h
  31. #define rightkey                4d00h
  32. #define upkey                   4800h
  33. #define downkey                 5000h
  34. #define retkey                  1c0dh
  35. #define esckey                  011bh
  36.  
  37. #define mouse                   51         /* interrupt 33h */
  38. #define resetmousedriver         0         /* reset mouse driver */
  39. #define pressmouse               5         /* mouse button press status */
  40. #define mousemotion             11         /* mouse cursor motion */
  41. #define seteventhandler         12         /* set mask and event handler */
  42. #define eventmask               0000000000001011b
  43. /* evenmask bits 0,1,4: movement, left & right press */
  44.  
  45. /*
  46.  Mouse driver resets to 8 and 16 respectively with function 00h.
  47.  Znix and Merit both worked well with settings 8/16.
  48. */
  49. int hsens=8,vsens=16;
  50. static int hpos=0;      /* store mouse cursor position X */
  51. static int vpos=0;      /* store mouse cursor position Y */
  52.  
  53. static
  54. stuffbuffer(){
  55. asm     cli                     /* Prevent interrupts from stuffing buffer */
  56. asm     push es                 /* typeahead buffer start at *[0040:0080] */
  57. asm     push bx                 /* and end at *[0040:0082]. */
  58.                                 /* At segment 40h and offset 01ah is */
  59. asm     mov di,40h              /* the offset into the typeahead buffer */
  60. asm     mov es,di               /* for the character at the head. At */
  61. asm     mov bx,es:[1ch]         /* segment 40h and offset 01ch is the */
  62. asm     mov si,bx               /* offset into the typeahead buffer for */
  63. asm     add bx,2                /* the last character. The buffer is full */
  64. asm     cmp bx,es:[1ah]         /* if *[01ch]+2 == *[01ah]. */
  65. asm     je quit                 /* Full? Then don't add any more chars */
  66. asm     cmp bx,es:[82h]         /* Offset less than end of buffer? */
  67. asm     jl isroom               /* Yes. Then go ahead and stuff it. */
  68. asm     mov bx,es:[80h]         /* Else wrap around to buffer start */
  69. isroom:
  70. asm     mov es:[si],cx          /* CX=scan code to be stuffed */
  71. asm     mov es:[1ch],bx         /* BX=new offset to last buffer character */
  72. quit:
  73. asm     pop bx
  74. asm     pop es
  75. asm     sti                     /* let other routines stuff the buffer */
  76. }
  77.  
  78. static
  79. manystuffbuffer(){
  80. /* AX=amount in mickeys mouse cursor has moved. */
  81. /* BX=sensitivity in mickeys */
  82. /* CX=key scan code to stuff into keyboard buffer */
  83. manyloop:
  84. asm     call stuffbuffer
  85. asm     sub ax,bx
  86. asm     cmp ax,bx
  87. asm     jge manyloop
  88. }
  89.  
  90. static
  91. far mouseeventhandler() {
  92. asm     sti
  93. asm     push ax
  94. asm     push bx
  95. asm     push cx
  96. asm     push dx
  97. asm     push ds                 /* ds=mouse driver data segment */
  98. asm     cld
  99. asm     mov ax, SEG _DATA       /* Get data segment for our data */
  100. asm     mov ds,ax               /* Set data segment */
  101. /*
  102.  Press Mouse supplies: BX=button status, CX=X mickeys, DX=Y mickeys
  103.                        The signed integers CX, DX measure UP==+, RIGHT==+
  104.                        Clears all button counts on each call.
  105. */
  106. asm     mov ax,pressmouse       /* get status of button press */
  107. asm     mov bx,0                /* for left button */
  108. asm     int mouse
  109. asm     and ax,1                /* Press left button? */
  110. asm     jz check_rightbutton    /* No? Then check right button. */
  111. asm     mov cx,retkey           /* Yes. Then plug in RETURN key. */
  112. asm     call stuffbuffer        /* near function call */
  113. check_rightbutton:
  114. asm     mov ax,pressmouse       /* get status of button press */
  115. asm     mov bx,2                /* for right button */
  116. asm     int mouse
  117. asm     and ax,2                /* press right button? */
  118. asm     jz checkcursor          /* No? Then check cursor keys. */
  119. asm     mov cx,esckey           /* Yes. Then plug in ESC key. */
  120. asm     call stuffbuffer        /* near function call */
  121. checkcursor:
  122. asm     mov ax,mousemotion      /* Has the mouse cursor moved? */
  123. asm     int mouse
  124. asm     mov ax,ds:hpos          /* save new horizontal position */
  125. asm     add ax,cx               /* cx=X coord supplied by mouse driver */
  126. asm     mov ds:hpos,ax
  127. asm     cmp ax,0                /* Has the mouse cursor moved? */
  128. asm     je  motionvertical      /* No change, then check vertical */
  129. asm     jg motionhorizontal     /* Make positive */
  130. asm     not ax
  131. motionhorizontal:
  132. asm     mov bx,ds:hsens
  133. asm     cmp ax,bx
  134. asm     jl motionvertical       /* didn't move enough for a change */
  135. asm     cmp ds:hpos,0
  136. asm     mov cx,rightkey
  137. asm     jg  stuffit1            /* If positive, then stuff cursor Right */
  138. asm     mov cx,leftkey
  139. stuffit1:
  140. asm     jmp stuffit2
  141. motionvertical:
  142. asm     mov ax,ds:vpos          /* save new vertical position */
  143. asm     add ax,dx               /* dx=Y coord supplied by mouse driver */
  144. asm     mov ds:vpos,ax
  145. asm     cmp ax,0
  146. asm     je eventreturn          /* no change, all done */
  147. asm     jg testvertsens         /* If positive, then stuff cursor Up */
  148. asm     not ax
  149. testvertsens:
  150. asm     mov bx,ds:vsens
  151. asm     cmp ax,bx
  152. asm     jl eventreturn          /* didn't move enough for a change */
  153. asm     cmp ds:vpos,0
  154. asm     mov cx,downkey
  155. asm     jg stuffit2
  156. asm     mov cx,upkey
  157. stuffit2:
  158. asm     call manystuffbuffer    /* stuff key CX into keyboard buffer */
  159. asm     mov ds:vpos,0           /* reset vertical saved position */
  160. asm     mov ds:hpos,0           /* reset horizontal saved position */
  161. eventreturn:
  162. asm     pop ds
  163. asm     pop dx
  164. asm     pop cx
  165. asm     pop bx
  166. asm     pop ax
  167. }
  168.  
  169. /* Returns 1 if it worked, 0 if it didn't */
  170. mousereset(){
  171. asm     mov ax,hsens            /* Don't hookup mouse if hsens==0 */
  172. asm     cmp ax,0                /* hsens,vsens set in PI.SET */
  173. asm     je nomouse
  174. asm     mov ax,resetmousedriver /* reset mouse driver to defaults */
  175. asm     int mouse
  176. asm     cmp ax,0                /* bx=number of buttons, not used yet */
  177. asm     mov ax,0
  178. asm     je nomouse              /* exit if no mouse driver loaded */
  179. asm     mov ax,1
  180. nomouse:;
  181. }
  182.  
  183. mousehook(){
  184. asm     mov ax,cs               /* Hook mouse driver to event handler */
  185. asm     mov es,ax               /* es=segment of event handler */
  186. asm     mov dx,offset mouseeventhandler
  187. asm     mov ax,seteventhandler  /* mouse driver function code */
  188. asm     mov cx,eventmask        /* signal mask for interrupt */
  189. asm     int mouse
  190. }
  191.  
  192. static int vsensx,hsensx;
  193.  
  194. /* Slow down the mouse in menus */
  195. menumouse(flag) int flag; {
  196.   if(flag){
  197.     vsensx=vsens; hsensx=hsens;
  198.     vsens= 2*vsens; hsens= 8*hsens;
  199.   } else {
  200.     vsens=vsensx; hsens=hsensx;
  201.   }
  202. }
  203.  
  204. #else
  205. menumouse(){}
  206. #endif
  207.